package com.itheima.sc.web.ums;

import com.itheima.sc.pojo.ums.UmsMenu;
import com.itheima.sc.result.Result;
import com.itheima.sc.service.ums.UmsMenuService;
import com.itheima.sc.vo.PageResult;
import com.itheima.sc.vo.UmsMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class UmsMenuController {
    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 添加后台菜单
     */
    @PostMapping("create")
    public Result create(@RequestBody UmsMenu umsMenu) {
        umsMenuService.create(umsMenu);
        return Result.success(null);
    }

    /**
     * 根据ID删除后台菜单
     */
    @PostMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        umsMenuService.delete(id);
        return Result.success(null);
    }

    /**
     * 分页查询后台菜单
     */
    @GetMapping("list/{parentId}")
    public Result list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, @PathVariable Integer parentId) {
        PageResult pageResult = umsMenuService.list(pageNum, pageSize, parentId);
        return Result.success(pageResult);
    }

    /**
     * 树形结构返回所有菜单列表
     */
    @GetMapping("treeList")
    public Result treeList() {
        List<UmsMenuVo> list = umsMenuService.treeList();
        return Result.success(list);
    }

    /**
     * 修改后台菜单
     */
    @PostMapping("update/{id}")
    public Result update(@RequestBody UmsMenu umsMenu) {
        umsMenuService.update(umsMenu);
        return Result.success(null);
    }

    /**
     * 修改菜单显示状态
     */
    @PostMapping("updateHidden/{id}")
    public Result updateHidden(@PathVariable Integer id,Integer hidden) {
        umsMenuService.updateHidden(id,hidden);
        return Result.success(null);
    }

    /**
     * 根据ID获取菜单详情
     */
    @GetMapping("{id}")
    public Result getMenuInfo(@PathVariable Integer id) {
        UmsMenu umsMenu = umsMenuService.getMenuInfo(id);
        return Result.success(umsMenu);
    }
}
